package com.sfh.base.http.server;

import android.text.TextUtils;
import android.util.SparseArray;


import com.sfh.base.http.IRxHttpClient;

import java.io.File;
import java.util.Iterator;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Retrofit;

/**
 * 功能描述:网络配置
 * 1.配置数据解析
 * 2.网络连接超时
 *
 * @date 2016/11/14
 */

public class RxHttpClient implements IRxHttpClient {



    public static class SingleTonHolder  {

        private static final RxHttpClient INSTANCE = new RxHttpClient ();
        public static  <T> T create(Class<T> service){
            return  INSTANCE.create (service);
        }
    }

    /*** 网络初始化 */
    public static RxHttpClient build(String host, boolean isDebug) {

        RxHttpClient client = SingleTonHolder.INSTANCE;
        client.init (host, isDebug);
        return client;
    }


    private Retrofit retrofit;

    // 网络请求接口
    private SparseArray<Object> server = new SparseArray<> ();

    private RxHttpClient() {

    }

    private void init(String host, boolean isDebug) {

        if (TextUtils.isEmpty (host)) {
            throw new RuntimeException ("RxHttpClient's host can't be empty");
        }
        this.server.clear ();
        this.retrofit = new OkHttpClientBuilder (host, isDebug).build ();
    }

    @Override
    public <T> T create(Class<T> service) {

        Object object = server.get (service.getName ().hashCode ());
        if (object != null) {
            return (T) object;
        }
        T t = this.retrofit.create (service);
        server.put (service.getName ().hashCode (), t);
        return t;
    }



    /***
     * 文件上传参数
     * @return
     */
    public static MultipartBody.Builder buildMultiParts(Map<String, Object> params) {
        //准备实体
        MultipartBody.Builder builder = new MultipartBody.Builder ();
        Iterator iterator = params.entrySet ().iterator ();
        while (iterator.hasNext ()) {
            Map.Entry<String, Object> entry = (Map.Entry) iterator.next ();
            Object value = entry.getValue ();

            if (value == null) {
                continue;
            }

            String key = entry.getKey ();
            if (value instanceof File) {
                File f = (File) value;
                RequestBody fileRequestBody = RequestBody.create (MediaType.parse ("application/octet-stream"), f);
                builder.addPart (MultipartBody.Part.createFormData (key, f.getName (), fileRequestBody));
            } else {
                builder.addPart (MultipartBody.Part.createFormData (key, String.valueOf (value)));
            }
        }
        builder.setType (MultipartBody.FORM);
        return builder;
    }


    /***
     * 图文文件上传参数
     * @return
     */
    public static MultipartBody.Builder buildMultiParts2(String key, File f) {
        //准备实体
        MultipartBody.Builder builder = new MultipartBody.Builder ();
        builder.setType (MultipartBody.FORM);
        RequestBody fileRequestBody = RequestBody.create (MediaType.parse ("application/octet-stream"), f);
        builder.addPart (MultipartBody.Part.createFormData (key, f.getName (), fileRequestBody));
        return builder;
    }
}
